﻿using d1Common.WebController;
using d1Project.Modules.Business.doXZ.Models;
using d1Project.Tools;
using d1Project.Tools.PagedList;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;

namespace d1Project.Modules.Business.doXZ.Controllers.a1
{
    /// <summary>
    /// 公式维护
    /// </summary>
    [Route("a1/XZ_a1_Gswh")]
    public class XZ_a1_GswhController : ControllerBase
    {
        protected override DbContext dbContext
        {
            get
            {
                return this.db;
            }
        }
        private doXZDbContext db = new doXZDbContext();
        private XZ_GswhModel salary = new XZ_GswhModel();

        /// <summary>
        /// 公式维护列表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<IHttpActionResult> Get([FromUri]XZ_GswhModelGetVm model)
        {
            var entity = db.XZ_Gswh.AsQueryable();
            if (model != null)
            {

            }
            var result = await entity.OrderByDescending(p => p.Id).ToPagedListAsync(model);
            return Ok(new { total = entity.Count(), rows = result });
        }
        /// <summary>
        /// 通过id查询相关信息
        /// </summary>
        /// <param name="id">序号</param>
        /// <returns></returns>
        public async Task<IHttpActionResult> Get(int id)
        {
            var entity = await db.XZ_Gswh.Where(t => t.Id == id).Select(t => new
            {
                t.Id,
                t.Name,
                t.Num
            }).FirstOrDefaultAsync();
            if (entity == null) this.Error("查询数据不存在");
            return Ok(entity);
        }
        /// <summary>
        /// 公式常数添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<IHttpActionResult> Post(XZ_GswhModelPostVm model)
        {
            if (!ModelState.IsValid) this.Error("请求参数不正确");
            XZ_GswhModel entity = new XZ_GswhModel();
           
            entity.Name = model.Name;
            entity.Num = model.Num;
            db.XZ_Gswh.Add(entity);
            await db.SaveChangesAsync();
            return Ok(new { name = 1 });
        }
        /// <summary>
        /// 公式常数编辑
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<IHttpActionResult> Put(XZ_GswhModelPutVm model)
        {
            if (!ModelState.IsValid) this.Error("请求参数不正确");
            //XZ_GswhModel entity = new XZ_GswhModel();
            var entity =await db.XZ_Gswh.FindAsync(model.Id);
            entity.Name = model.Name;
            entity.Num = model.Num; 
            await db.SaveChangesAsync();
            return Ok(new { name = 1 });
        }
        /// <summary>
        /// 删除公式中常数
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<IHttpActionResult> Delete(int id)
        {
            var entity = await db.XZ_Gswh.FindAsync(id);
            db.XZ_Gswh.Remove(entity);
            await db.SaveChangesAsync();
            return Ok(new { name = 1 });
        }
    }
    public class XZ_GswhModelPostVm
    {
        /// <summary>
        /// 编号
        /// </summary>
        public int Id { get; set; }
        public string Name { get; set; }
        public double Num { get; set; }
    }
    public class XZ_GswhModelPutVm
    {
        /// <summary>
        /// 编号
        /// </summary>
        public int Id { get; set; }
        public string Name { get; set; }
        public double Num { get; set; }
    }
    public class XZ_GswhModelGetVm:VmBase
    {
        /// <summary>
        /// 编号
        /// </summary>
        public int Id { get; set; }
        public string Name { get; set; }
        public double Num { get; set; }
    }
}
